// @filename   im.js
// @version    0.2
// @author     shawphy(shawphy@gmail.com),fantasy(foxlw66@gmail.com)
// @update     2009-9-7

(function($) {
	//全局变量
	var GET_FRIS_URL = site_url+"/index.php?s=/Chat/getFriends2";
	var REFRESH_URL = site_url+"/index.php?s=/Chat/refreshOnline";
	var GO_CHAT_URL = site_url+"/index.php?s=/Chat/box";
	var MIN_PIC_URL = theme_url+"/im/images/minimize.gif";
	var MAX_PIC_URL = theme_url+"/im/images/maximize.gif";
	var CHECK_CHAT_MSG = site_url+"/index.php?s=/Chat/checkChatMsg";
	var CHECK_CHAT_ID ;
	var CHECK_TIME = 8000;
	var CHAT_NUM = 0;



	var	imbox=$("#imbox");
	//绑定最大化最小化,两个页面通用
	imbox.find("h1").find("span,em").click(function() {
		if (imbox.hasClass("min")) {
			//隐藏消息提示
			imnotify.hide();
			$("#im_ico_msg").hide();
			//在线好友列出来
			imbox.removeClass("min");
			//把右上角的图片改为最小化
			$("#right_top").attr("src",MIN_PIC_URL);
			//loading;
			$("#friend_list").append("<li>正在加载中...</li>")
			//设置窗口高度
			$("#friend_list").parent().height($("#friend_list").height()<402?$("#friend_list").height():402);
			$.post(GET_FRIS_URL,{chat_num:CHAT_NUM},function(json){
                if(json){
                        //构造在线好友列表
                        $("#friend_list").empty();
                        var online_num = 0;
                        for(var i=0,h=json.length;i<h;i++){
                                 online_num++;
                                 var head = json[i]["head"];
								 
                                 var mini = json[i]["mini"]?'('+json[i]["mini"]+')':'';
								 var chat_num = json[i]["chat_num"]?'<font color=red><b>('+json[i]["chat_num"]+')</b></font>':"";
                                 $("#friend_list").append('<li><a href="#" id="'+json[i]["uid"]+'"><img width="25" src="'+head+'" /><span class="name">'+json[i]["uname"]+'</span><span>'+mini+chat_num+'</span></a></li>');
                         }
                        //在线好友数
                         $("#online_num").text('在线好友('+online_num+')');

                        //重复绑定
                        openChatBind();
                        //设置窗口高度
                        $("#friend_list").parent().height($("#friend_list").height()<402?$("#friend_list").height()+5:402);
						//取消轮询消息
						


                }else{
                     $("#friend_list").html("<li>目前没有好友在线!</li>");
                }

			},"json");
			clearInterval(CHECK_CHAT_ID);
		}else{
			imbox.addClass("min");
			$(this).find("img").attr("src",MAX_PIC_URL);
			$("#friend_list").html('');
			//轮询接收，4s一次
		    CHECK_CHAT_ID = setInterval(checkChatMsg, CHECK_TIME);
		};
	});

	//普通页弹出窗口
	var	imboxWindow;//定义窗口
	var	openChatBind=function()	{
		$("#friend_list>li>a").click(function(evt){
			evt.preventDefault();
			try	{
				imboxWindow.focus();
				imboxWindow.$("#"+this.id).click();
				//imboxWindow.$("#open_df_id").val(this.id);
			}
			catch (err)	{
				imboxWindow=window.open(GO_CHAT_URL+"#"+this.id,"imboxWindow","height=530, width=696, toolbar=no, top="+(screen.height/2-350)+", left="+(screen.width/2-500)+",	menubar=no,	scrollbars=no,resizable=no,location=no,	status=no");
				//imboxWindow.$("#"+this.id).click();
			}
		});
	}
	openChatBind();

    $.post(REFRESH_URL,{},function(txt){});

	var imnotify=$("#im_notification");
	imnotify.twinkle=function(num) {

		this.find("span").html(num).end().show()
			.animate({"bottom":"+=10px"},150)
			.animate({"bottom":"-=10px"},150)
			.wait(600)
			.animate({"bottom":"+=10px"},150)
			.animate({"bottom":"-=10px"},150)
			.wait(600)
			.animate({"bottom":"+=10px"},150)
			.animate({"bottom":"-=10px"},150);

	}
	$.fn.wait = function(time, type) {
		time = time || 1000;
		type = type || "fx";
		return this.queue(type, function() {
			var self = this;
			setTimeout(function() {
				$(self).dequeue();
			}, time);
		});
	};


    //轮询接收，4s一次
    CHECK_CHAT_ID = setInterval(checkChatMsg, CHECK_TIME);

	//接收聊天信息
	function checkChatMsg(){

		 $.post(CHECK_CHAT_MSG, null, function(num){
	 	  if (num>0) {
			    CHAT_NUM = num;
				imnotify.twinkle(num);
				$("#im_ico_msg").show();
				if(!g_blinkid) g_blinkid = setInterval(blinkNewChat,1000);
			}
		  });
	    
	}

	/*-------------------------------------
	= title提示
	-------------------------------------*/
	var g_blinkswitch = 0;
	var g_blinktitle = document.title;
	var g_blinkid;

	//title闪烁"你有新的聊天消息"
	function blinkNewChat(){
		document.title = g_blinkswitch % 2 ? "○你有新的聊天消息 - " + g_blinktitle : "●你有新的聊天消息 - " + g_blinktitle;
		g_blinkswitch++;
		(g_blinkswitch > 6)?stopBlinkNewMsg():'';
	}


	//停止title闪烁
	function stopBlinkNewMsg(){
		if (g_blinkid){
			clearInterval(g_blinkid);
			g_blinkid = 0;
			g_blinkswitch = 0;
			document.title = g_blinktitle;
		}
	}


})(jQuery);


